<template>
  <a-card title="" :bordered="false">
    <a-spin :spinning="loading" v-if="report.dataId">
      <a-page-header style="border-bottom: 1px solid rgb(235, 237, 240)">
        <template slot="title">
          <h1>{{ report.reportName }}</h1>
          <span :style="{ marginRight: '10px', fontWeight: 'normal', fontSize: '16px' }"
            >报告时间：{{ report.reportCreateTime }}</span
          >
          <span v-if="report.publishStatus != 0" :style="{ fontWeight: 'normal', fontSize: '16px' }"
            >发布时间：{{ report.reportCreateTime }}</span
          >
        </template>
        <template slot="subTitle">
          <a-badge :status="report.publishStatus == 0 ? 'warning' : 'success'" />
          {{ report.publishStatus == 0 ? '待发布' : '已发布' }}
        </template>
        <template slot="extra">
          <!-- <a-popconfirm placement="topLeft" ok-text="确定" cancel-text="取消" @confirm="removeWeekReport">
            <template slot="title">
              <p>你确定要删除这篇报告吗？</p>
            </template>
            <a-button style="margin-left: 30px" :loading="deleteLoading">删除报告</a-button>
          </a-popconfirm> -->
          <a-button key="打印报告" type="primary" @click="printReport"> 打印报告 </a-button>
        </template>
      </a-page-header>
      <div :style="{ marginTop: '15px' }" ref="printReport">
        <cover v-if="coverLoading" :report="report"></cover>
        <basic-info :type="$route.query.type" :report="report" :status="report.publishStatus"></basic-info>
        <!-- <service-programme :report="report.reportContent.serviceProgramme"></service-programme>
        <signin-record
          :report="report.reportContent.dailySigninRecords"
          :weeklySigninRate="report.reportContent.weeklySigninRate"
        ></signin-record> -->
        <punch-rate-compliance-rate
          :report="report.reportContent.signinRateLine"
          v-if="$route.query.type == 2"
        ></punch-rate-compliance-rate>
        <target
          :type="$route.query.type"
          :status="report.publishStatus"
          :report="report.reportContent.controllerTargetReportList"
        ></target>
        <trend :type="$route.query.type" :report="report.reportContent" v-if="isNotEmptyTrend"></trend>
        <Body v-if="$route.query.type == 2" :report="report" :status="report.publishStatus"></Body>
        <suggests :type="$route.query.type" :report="report.reportContent.suggests || {}"></suggests>
        <report
          v-if="
            report.publishStatus == 0 ||
              (report.publishStatus == 1 &&
                ($route.query.type == 1
                  ? report.weekInputUpdate.reportPictures
                  : report.monthInputUpdate.reportPictures))
          "
          :report="$route.query.type == 1 ? report.weekInputUpdate : report.monthInputUpdate"
          :status="report.publishStatus"
        ></report>
        <total :report="report" :status="report.publishStatus"></total>
        <a-row>
          <a-col :span="6">
            <div :style="{ fontSize: '16px', margin: '25px 0 5px 0', fontWeight: 600 }">
              <span style="margin-right: 50px">制表人：{{ report.creator }}</span>
            </div>
          </a-col>
          <a-col :span="6">
            <div :style="{ fontSize: '16px', margin: '25px 0 5px 0', fontWeight: 600 }">
              <span>日期：{{ report.reportCreateTime }}</span>
            </div>
          </a-col>
        </a-row>
        <a-row v-if="report.publishStatus == 1">
          <a-col :span="6">
            <div :style="{ fontSize: '16px', margin: '25px 0 5px 0', fontWeight: 600 }">
              <span style="">患者签名：</span>
            </div>
          </a-col>
          <a-col :span="6">
            <div :style="{ fontSize: '16px', margin: '25px 0 5px 0', fontWeight: 600 }">
              <span>医生签名：</span>
            </div>
          </a-col>
        </a-row>
      </div>
      <!-- <print-report></print-report> -->
      <a-row v-if="report.publishStatus == 0">
        <a-col :span="4" :offset="20">
          <a-button :style="{ margin: '15px 0' }" type="primary" :loading="publishLoading" @click="publishReport(1)"
            >发布</a-button
          >
        </a-col>
      </a-row>
      <a-row v-if="report.publishStatus == 1">
        <a-col :span="4" :offset="20">
          <a-button :style="{ margin: '15px 0' }" type="primary" :loading="publishLoading" @click="publishReport(0)"
            >取消发布</a-button
          >
        </a-col>
      </a-row>
    </a-spin>
    <a-empty v-else></a-empty>
  </a-card>
</template>

<script>
import { PAGE_MENU_TYPE } from '@/store/mutation-types'
import { getAction, postAction, deleteAction } from '@/api/manage'
import BasicInfo from './modules/Week/BasicInfo.vue'
import ServiceProgramme from './modules/First/ServiceProgramme.vue'
import Report from './modules/First/Report.vue'
import SigninRecord from './modules/Week/SigninRecord.vue'
import Target from './modules/First/Target.vue'
import Suggests from './modules/Week/Suggests.vue'
import Body from './modules/Week/Body.vue'
import Total from './modules/Week/Total.vue'
import Trend from './modules/Week/Trend.vue'
import PrintReport from './modules/PrintReport/PrintReport.vue'
import PunchRateComplianceRate from './modules/Final/PunchRateComplianceRate.vue'
import print from '@/utils/print'
import Cover from './modules/First/Cover.vue'

export default {
  name: 'ViewReport',
  components: {
    BasicInfo,
    ServiceProgramme,
    SigninRecord,
    Target,
    Suggests,
    PrintReport,
    Total,
    Report,
    Trend,
    Body,
    PunchRateComplianceRate,
    Cover
  },
  data() {
    return {
      loading: true,
      deleteLoading: false,
      publishLoading: false,
      url: {
        getWeekReport: 'patient/report/week/get',
        getMonthReport: 'patient/report/month/get',
        removeWeekReport: 'patient/report/remove'
      },
      report: {},
      visible1: false,
      coverLoading: false
    }
  },
  computed: {
    isNotEmptyTrend() {
      const sugarChart = this.report.reportContent.sugarChart?.value || []
      const list = [...sugarChart[1], ...sugarChart[2], ...sugarChart[4], ...sugarChart[6]].filter(v => {
        return v !== "''"
      })
      const periodBloodPressure = this.report.reportContent.periodBloodPressure?.dates || []
      const periodBmi = this.report.reportContent.periodBmi?.dates || []
      return (
        (sugarChart || periodBloodPressure || periodBmi) &&
        (list.length > 0 || periodBloodPressure.length > 0 || periodBmi.length > 0)
      )
    }
  },
  created() {
    this.getWeekReport()
  },
  methods: {
    printReport() {
      if (this.report.publishStatus != 1) {
        this.$message.warning('请先发布报告！')
        return
      }
      this.coverLoading = true
      //    this.$message.info('coding')
      this.$nextTick(() => {
        print(this.$refs.printReport)
        this.coverLoading = false
      })
    },

    getWeekReport() {
      this.loading = true
      getAction(this.$route.query.type == 1 ? this.url.getWeekReport : this.url.getMonthReport, {
        id: this.$route.query.reportId
      })
        .then(res => {
          if (res.result != null && res.result.dataId) {
            this.report = res.result
            this.report.reportContent.controllerTargetReportList = res.result.reportContent.controllerTargetReportList.filter(
              v => {
                return v.typeId != 14
              }
            )
          }
        })
        .finally(() => {
          this.loading = false
        })
    },
    removeWeekReport() {
      if (this.$route.query.reportId) {
        this.deleteLoading = true
        deleteAction(this.url.removeWeekReport, {
          id: this.$route.query.reportId
        })
          .then(res => {
            if (res.success) {
              this.$message.success('周报删除成功')
              this.getWeekReport()
            } else {
              this.$message.error(res.message)
            }
          })
          .finally(_ => {
            this.deleteLoading = false
          })
      } else {
        this.$message.error('数据异常，无法操作')
      }
    },
    publishReport(status) {
      this.publishLoading = true
      if (this.$route.query.type == 1) {
        postAction('patient/report/week/update', {
          reportId: this.$route.query.reportId,
          publishStatus: status, // 1-发布，0-未发布
          reportPictures: this.report.weekInputUpdate.reportPictures,
          mainQuestion: this.report.weekInputUpdate.mainQuestion,
          summary: this.report.summary
        })
          .then(res => {
            if (res.success) {
              this.$message.success('周报发布成功')
              this.getWeekReport()
            } else {
              this.$message.error(res.message)
            }
          })
          .finally(_ => {
            this.publishLoading = false
          })
      } else {
        let laboratoryList = {}
        this.report.reportContent.controllerTargetReportList.forEach(v => {
          if (![1, 2, 3, 5, 6, 7, 8, 9].includes(v.typeId)) {
            laboratoryList[JSON.stringify(v.typeId)] = Number(v.thisPeriodValue)
          }
        })
        postAction('patient/report/month/update', {
          reportId: this.$route.query.reportId,
          publishStatus: status, // 1-发布，0-未发布
          reportPictures: this.report.monthInputUpdate.reportPictures,
          laboratoryList,
          mainQuestion: this.report.monthInputUpdate.mainQuestion,
          bodyPictures: this.report.monthInputUpdate.bodyPictures,
          summary: this.report.summary
        })
          .then(res => {
            if (res.success) {
              this.$message.success('月报发布成功')
              this.getWeekReport()
            } else {
              this.$message.error(res.message)
            }
          })
          .finally(_ => {
            this.publishLoading = false
          })
      }
    }
  }
}
</script>

<style lang="less" scoped>
/deep/ .ant-descriptions-title {
  margin-bottom: 1px;
  background: rgb(245, 34, 45);
  color: rgb(255, 255, 255);
  padding: 20px;
}
/deep/ .ant-descriptions-item-label {
  width: 25%;
}
/deep/ .ant-descriptions-item-content {
  width: 25%;
}
</style>

